'use strict';
const crypto = require('crypto')
if(!global.SESSION) global.SESSION = {};
module.exports = (request, response, rfun)=>{
    request.COOKIE = request.PUBFUN.getCookie(request);
    if(!("tjsid" in request.COOKIE)){
        let md5 = crypto.createHash("md5");
        md5.update(new Date().getTime().toString());
        request.COOKIE["tjsid"] = md5.digest('hex');
        response.setHeader("Set-Cookie", "tjsid="+request.COOKIE["tjsid"]+";path=/");
        //SESSION[request.COOKIE["tjsid"]] = {"18243":"123456"}; //测试代码
    }
    for(let k in SESSION){
        if(Date.now() - SESSION[k].T > 3600000) delete SESSION[k];
    }
    request.SESSION = SESSION[request.COOKIE["tjsid"]];
    if(!request.SESSION){
        request.SESSION = SESSION[request.COOKIE["tjsid"]] = {T:Date.now()};
    }else{
        request.SESSION.T = Date.now();
    }
    //console.log(SESSION, request.COOKIE["tjsid"], request.SESSION);
    rfun();
}